(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('slowSvcController', slowSvcController)
    slowSvcController.$inject = ['$scope', 'searchService', 'logmanagerService'];

    function slowSvcController($scope, searchService, logmanagerService) {
        $scope.slowSvcarray = [];
        $scope.partition = '所有';
        $scope.code = "";
        $scope.svcData = [];
        $scope.sqlData = [];
        $scope.showData = [];
        $scope.show = [];
        $scope.partitionName = '';
        $scope.loading="false";


      // $scope.treeData =[];

        $scope.treeOptions = {
            select: onSelect
        }
        $scope.param = {
            time: "500",
            startTime: getToday00Clock(),
            endTime: getToday11Clock(),

        };
        $scope.treeTemplate = kendo.template($("#treeview-template").html());
        $scope.svcGridOptions = {
            serverSorting: true,
            selectable: "row ",
            scrollable: true,
            sortable: true,
            resizable: true,
            toolbar: ["excel"],
            excel: {
                fileName: "慢服务.xlsx",
                filterable: true
            }
        };
        $scope.columns = [
            {field: "time ", title: "Time", width: 200},
            {field: "partition ", title: "分区名", width: 200},
            {field: "service ", title: "服务名", width: 200},
            {field: "execTime ", title: "耗时(ms)", width: 200}
        ];
        $scope.sqlcolumns = [
            {field: "time ", title: "Time", width: 200},
            {field: "logmsg ", title: "logmsg", width: 800},
            {field: "partition ", title: "分区名", width: 100},
            {field: "service ", title: "服务名", width: 200},
            {field: "execTime ", title: "耗时(ms)", width: 100}
        ];

        $scope.sqlGridOptions = {
            selectable: "row",
            scrollable: true,
            sortable: true,
            resizable: true,
            toolbar: ["excel"],
            excel: {
                fileName: "慢Sql.xlsx",
                filterable: true
            }
        };
        searchService.getPartition({}).then(
            function (resp) {
                $scope.partitionSource = toTree(resp.data);
                $scope.partition = resp.data[0].fname + '-' + resp.data[0].cname;
                $scope.partitionName = resp.data[0].ename;
                $scope.code = resp.data[0].code;
                angular.forEach(resp.data, function (item) {
                    $scope.show.push(item.ename);
                });
                $scope.treeData = {
                    transport: {
                        read: function (options) {
                            var parentName = options.data.SubNodes || "root";
                            console.log($scope.partition)
                            console.log($scope.partitionName)
                            $scope.startTime = $scope.param.startTime.Format("yyyy-MM-ddThh:mm:ss.S");
                            $scope.endTime = $scope.param.endTime.Format("yyyy-MM-ddThh:mm:ss.S");
                            if (parentName == "root") {
                                // $scope.svcData = [];
                                logmanagerService.findSlowSvc($scope.partitionName, $scope.startTime, $scope.endTime, $scope.param.time, $scope.code).then(function (resp) {
                                    var data = resp;
                                    console.log(resp);
                                    $scope.treeviewData = data;
                                    options.success($.grep($scope.treeviewData, function (x) {
                                        return x.Name == "root";
                                    }));
                                }, function (error) {
                                    console.log(error);
                                });
                            }
                            else {
                                options.success($.grep($scope.treeviewData, function (x) {
                                    return x.Name == parentName;
                                }));
                            }

                        }
                    },
                    requestStart: function (e) {
                        if ( $scope.loading=="false"){
                            e.preventDefault();
                        }
                    },
                    schema: {
                        model: {
                            id: "SubNodes",
                            hasChildren: function (x) {
                                var name = x.SubNodes;
                                for (var i = 0; i < $scope.treeviewData.length; i++) {
                                    if ($scope.treeviewData[i].Name == name) {
                                        return true;
                                    }
                                }
                                return false;
                            }
                        }

                    }
                };
            }, function (err) {
            }
        );



        $scope.selectShow = function (k) {
            angular.forEach($scope.show, function (item) {
                if (item == k.ename) {
                    $scope.partition = k.fname + "-" + k.cname;
                    $scope.partitionName = k.ename;
                    $scope.code = k.code;
                }
            });
            $scope.win.close();
        };


        function onSelect(e) {
            $scope.svcData = [];
            var dataItem = this.dataItem(e.node);
            if (dataItem.SubNodes.length != 2) {
                logmanagerService.findSvc(dataItem.SubNodes.split("【")[0]).then(function (resp) {
                    $scope.svcData = resp;
                    console.log(resp);
                }, function (error) {
                    console.log(error);
                });
            }
            // $scope.s = $scope.showData;
            // console.log($scope.s);
        }



        $scope.gridRowClick = function (selected, data, dataItem, angularDataItem) {
            $scope.sqlData = [];
            $scope.startTime = $scope.param.startTime.Format("yyyy-MM-ddThh:mm:ss.S");
            $scope.endTime = dataItem.time.replace(" ", "T");
            $scope.parti = dataItem.partition;
            $scope.svc = dataItem.service;
            logmanagerService.findSlowSql($scope.startTime, $scope.endTime, $scope.parti, $scope.svc, $scope.code).then(function (resp) {
                $scope.sqlData = resp;
                console.log(resp);
            }, function (error) {
                console.log(error);
            });
        }



        $scope.query = function () {

            $scope.sqlData = [];
            $scope.svcData = [];
            $scope.loading="true";

            $scope.tree.dataSource.read();
        }



    }


})();
